/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.editpolicies.DirectEditPolicy;

import edu.pku.sei.modeler.editpolicies.NestedNodeLayoutEditPolicy;
import edu.pku.sei.modeler.editpolicies.UMLDiagramGraphicalNodeEditPolicy;
import edu.pku.sei.modeler.figures.UMLPackageFigure;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.model.structure.StructurePackage;
import edu.pku.sei.modeler.model.uml.Kernel.UMLPackage;
import edu.pku.sei.modeler.uml.modelHelper.ModelPackage;

/**
 * @generated
 * 
 */
public class UMLPackageEditpart extends AbstractUMLNodeEditPart {

	public UMLPackageEditpart(MetaShapeElement model) {
		super(model);
	}

	private UMLPackage getModelElement() {
		return (UMLPackage) ((MetaNode) getModel()).getModelElement();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		return new UMLPackageFigure();
	}

	protected DirectEditPolicy createDirectEditPolicy() {
		// return new DirectEditPolicy() {
		// protected Command getDirectEditCommand(DirectEditRequest request) {
		// return new ChangeNameCommand(getUMLPackage(), (String) request
		// .getCellEditor().getValue());
		// }
		//
		// protected void showCurrentEditValue(DirectEditRequest request) {
		// IFigure fig = getDirectEditFigure();
		// if (fig instanceof Label) {
		// ((Label) fig).setText(getUMLPackage().getName());
		// fig.getUpdateManager().performUpdate();
		// }
		// }
		// };
		return null;
	}

	@Override
	protected IFigure getDirectEditFigure() {
		// return ((UMLPackageFigure) this.getFigure()).getPackageNameLabel();
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE,
				new NestedNodeLayoutEditPolicy(((MetaNode)this.getModel()).getDiagram().getType()));

		// installActionHandle();
	}

	protected void handlePropertyChanged(PropertyChangeEvent event) {
		String propertyName = event.getPropertyName();
		// UMLPackage diagram = (UMLPackage)getModel();
		if (StructurePackage.DIAGRAM_NODES.equals(propertyName)) {
			refreshChildren();
		}
		// else if(StructurePackage.DIAGRAM_SHOWGRID.equals(propertyName)) {
		// getViewer().setProperty(SnapToGrid.PROPERTY_GRID_VISIBLE,
		// diagram.isShowGrid());
		// getViewer().setProperty(SnapToGrid.PROPERTY_GRID_ENABLED,
		// diagram.isShowGrid());
		// }
		// else if(StructurePackage.DIAGRAM_SNAPGEO.equals(propertyName)) {
		// getViewer().setProperty(SnapToGeometry.PROPERTY_SNAP_ENABLED,
		// diagram.isSnapGeo());
		// }
		// else if(StructurePackage.DIAGRAM_SHOWRULER.equals(propertyName)) {
		// getViewer().setProperty(RulerProvider.PROPERTY_RULER_VISIBILITY,
		// diagram.isShowRuler());
		// }

		refreshVisuals();
		super.handlePropertyChanged(event);
	}

	@Override
	public IFigure getContentPane() {
		if (getFigure() instanceof UMLPackageFigure) {
			return ((UMLPackageFigure) getFigure()).getContentPane();
		}

		return super.getFigure();
	}

	protected void refreshVisuals() {

		UMLPackage modelElement = (UMLPackage) getNode().getModelElement();
		UMLPackageFigure packageFigure = (UMLPackageFigure) getFigure();

		refreshPackageName(modelElement, packageFigure);

		super.refreshVisuals();
	}

	private void refreshPackageName(UMLPackage modelElement,
			UMLPackageFigure packageFigure) {
		String packageName = modelElement.getName();
		Label nameLabel = packageFigure.getPackageNameLabel();
		nameLabel.setText(packageName);
		// nameLabel.setTextAlignment(PositionConstants.TOP_MIDDLE_BOTTOM);

	}

	// protected void installActionHandle() {
	// // Install mouse hover
	// ActionHandleManager actionMngr = new ActionHandleManager(this);
	// // Create a toolbar of actions.
	// try {
	// AbstractFactory modelFactory = ModelRegistry.getRegistry()
	// .getFactory(this.getNode().getModel());
	//
	// StructureFactory sFactory = (StructureFactory) ModelRegistry
	// .getRegistry().getStructureFactory(
	// this.getNode().getModel());
	// PaletteDrawer entries = new PaletteDrawer("creation tool");
	// CreationToolEntry addPackageToolEntry = new CreationToolEntry(
	// ImageProvider.PACKAGE_IMAGE, "Add New Package",
	// new MetaCreationFactory(true, ModelPackage.__UMLPACKAGE__,
	// modelFactory, sFactory), ImageProvider
	// .getImageDescriptor(ImageProvider.PACKAGE_IMAGE),
	// null);
	//
	// CreationToolEntry addClassToolEntry = new CreationToolEntry(
	// ImageProvider.CLASS_IMAGE, "Add New Class",
	// new MetaCreationFactory(true, ModelPackage.__UMLCLASS__,
	// modelFactory, sFactory), ImageProvider
	// .getImageDescriptor(ImageProvider.CLASS_IMAGE),
	// null);
	//
	// entries.add(addPackageToolEntry);
	// entries.acceptsType(addClassToolEntry);
	//
	// actionMngr.addTool(entries, PositionConstants.SOUTH_EAST);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

}
